package com.ebt.m.config;



import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;

import com.ebt.m.utils.ConfigData;
/**
 * 读取assets文件夹下DataBaseVersion.xml文件配置的数据库EbtDB节点、Log节点、HelperDB节点。
 * @author Allen.li
 *
 */
public class DataBaseUpdateConfig {
	private static Map<String, List<String>> EbtDataConfigMaps = new HashMap<String, List<String>>();

	private static Map<String, List<String>> LogConfigMaps = new HashMap<String, List<String>>();
	
	private static  List<DBOptionItem> EbtDataConfigList = new  ArrayList<DBOptionItem>();
	private static  List<DBOptionItem> LogConfigList = new  ArrayList<DBOptionItem>();
	private static  List<DBOptionItem> helperConfigList = new  ArrayList<DBOptionItem>();

	public final static String ELEMENT_EBTDATA_TAG = "EbtDB";
	public final static String ELEMENT_LOGDATA_TAG = "Log";
	public final static String ELEMENT_HELPERDATA_TAG = "HelperDB";

	public final static String ELEMENT_VERION_TAG = "version";

	/** 新版本号标签 **/
	public final static String ELEMENT_NEW_VERSION_TAG = "newVersion";

	/** 操作标签 **/
	public final static String ELEMENT_ITEM_TAG = "item";

	private static String ebtNewVersion;
	
	private static String logNewVersion;
	private static String helperNewVersion;

	private static Document document;

	/**
	 * 系统运行时初始化配置文件
	 */
	public static void init(Context context) {
		DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		document = null;
		InputStream inputStream = null;
		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			inputStream = context.getResources().getAssets()
					.open("DataBaseVersion.xml");
			document = builder.parse(inputStream);
			Element root = document.getDocumentElement();
			initEbtDB(root);
			initLogDB(root);
			initHelperDB(root);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}


	private static void initLogDB(Element root) {
		try {
		LogConfigList.clear();
		NodeList nodes = document.getElementsByTagName(ELEMENT_LOGDATA_TAG);

		Element logDB = (Element) nodes.item(0);

		Element versionelement = (Element) logDB.getElementsByTagName(
				ELEMENT_NEW_VERSION_TAG).item(0);

		if (versionelement != null) {
			logNewVersion = versionelement.getAttribute("value");

		}

		NodeList items = logDB.getElementsByTagName(ELEMENT_VERION_TAG);

		for (int i = 0; i < items.getLength(); i++) {
			Element element = (Element) items.item(i);
			String key = element.getAttribute("value");
			NodeList itemNodes = element.getElementsByTagName(ELEMENT_ITEM_TAG);
			for (int j = 0; j < itemNodes.getLength(); j++) {
				
				DBOptionItem  optionitem = new DBOptionItem();
				Element item = (Element) itemNodes.item(j);
				
				optionitem.version =Integer.parseInt(key);
				optionitem.sql = item.getAttribute("value");

			
				LogConfigList.add(optionitem);
			}
		
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void initHelperDB(Element root) {
		try {
		helperConfigList.clear();
		NodeList nodes = document.getElementsByTagName(ELEMENT_HELPERDATA_TAG);

		Element logDB = (Element) nodes.item(0);

		Element versionelement = (Element) logDB.getElementsByTagName(
				ELEMENT_NEW_VERSION_TAG).item(0);

		if (versionelement != null) {
			helperNewVersion = versionelement.getAttribute("value");

		}

		NodeList items = logDB.getElementsByTagName(ELEMENT_VERION_TAG);

		for (int i = 0; i < items.getLength(); i++) {
			Element element = (Element) items.item(i);
			String key = element.getAttribute("value");
			NodeList itemNodes = element.getElementsByTagName(ELEMENT_ITEM_TAG);
			for (int j = 0; j < itemNodes.getLength(); j++) {
				
				DBOptionItem  optionitem = new DBOptionItem();
				Element item = (Element) itemNodes.item(j);
				
				optionitem.version =Integer.parseInt(key);
				optionitem.sql = item.getAttribute("value");

			
				helperConfigList.add(optionitem);
			}
		
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 初始化EBTDB 
	 * @param root
	 */
	private static void initEbtDB(Element root) {
		try {
		//先清除数据
		EbtDataConfigList.clear();
		NodeList nodes = document.getElementsByTagName(ELEMENT_EBTDATA_TAG);

		Element EbtDB = (Element) nodes.item(0);

		Element versionelement = (Element) EbtDB.getElementsByTagName(
				ELEMENT_NEW_VERSION_TAG).item(0);

		if (versionelement != null) {
			ebtNewVersion = versionelement.getAttribute("value");
		}

		NodeList items = EbtDB.getElementsByTagName(ELEMENT_VERION_TAG);

		for (int i = 0; i < items.getLength(); i++) {
			Element element = (Element) items.item(i);
			String key = element.getAttribute("value");
			NodeList itemNodes = element.getElementsByTagName(ELEMENT_ITEM_TAG);
		
			for (int j = 0; j < itemNodes.getLength(); j++) {
				DBOptionItem  optionitem = new DBOptionItem();
				Element item = (Element) itemNodes.item(j);
				
				optionitem.version =Integer.parseInt(key);
				optionitem.sql = item.getAttribute("value");
				
				EbtDataConfigList.add(optionitem);

			}
			
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取ebtDB，需要更新到的版本号
	 * 
	 * @return
	 */
	public static String getEbtNewVersion() {
		return ebtNewVersion;
	}
	/**
	 * 获取logDB，需要更新到的版本号
	 * 
	 * @return
	 */
	public static String getLogNewVersion() {
		return logNewVersion;
	}
	/**
	 * 获取ebtDB，需要更新到的版本号
	 * 
	 * @return
	 */
	public static String getHelperNewVersion() {
		return helperNewVersion;
	}
	/**
	 * 获取更新Ebt数据库版本，所需要的操作(按顺序排列).包含version的也要执行
	 * @param version
	 * @return
	 */
	public static List<DBOptionItem>  getEbtOptionsByVersion( int version){
		return getOptionsByVersion(EbtDataConfigList,version);
	}
	/**
	 * 获取更新Log数据库版本，所需要的操作(按顺序排列)
	 * @param version
	 * @return
	 */
	public static  List<DBOptionItem>  getLogOptionsByVersion( int version){
		return getOptionsByVersion(LogConfigList,version);
	}
	/**
	 * 获取更新版本，所需要的操作(按顺序排列)
	 * 
	 * @return
	 */
	public static List<DBOptionItem> getOptionsByVersion(
			List<DBOptionItem> list, int version) {
		List<DBOptionItem> result = new ArrayList<DBOptionItem>();
		for (DBOptionItem item : list) {
			
				if (item.version > version) {
					result.add(item);
				}
		}
		return result;

	}
}
